package dataStructure.xmg1.a03;

/**
 * @author: vince_zww
 * @date: 2020/10/18 0:17
 * @desceription:
 */
public class ArrayList<E> extends AbstractList<E> {

    /**
     * 所有的元素
     *
     */
    private E[] elements;

    //默认值
    private static final int DEFAULT_CAPACITY = 10;

    /**
     *
     * @param capaticy 初始容量
     */
    public ArrayList(int capaticy){
        //传入初始值太小，就取默认值
        capaticy = (capaticy < DEFAULT_CAPACITY) ? DEFAULT_CAPACITY : capaticy;
        elements = (E[]) new Object[capaticy];//这里创建时候只是创建足够存放内存地址的容量！
    }

    public ArrayList(){
        this(DEFAULT_CAPACITY);
    }

    /**
     * 清除所有元素
     */
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }


    /**
     * 获取index位置的元素
     * @param index
     * @return
     */
    public E get(int index) { //O(1)
        rangeCheck(index);
        return elements[index];
    }

    /**
     * 设置index位置的元素
     * @param index
     * @param element
     * @return 原来的元素
     */
    public E set(int index, E element) { //O(1)
        rangeCheck(index);
        E old = elements[index];
        elements[index] = element;
        return old;
    }

    /**
     * 在index位置插入一个元素
     * @param index
     * @param element
     */
    public void add(int index, E element) { //O(n)
//        if (element ==null) return;
        /**
         * 最好：O(1)
         * 最坏：O(n)
         * 平均：O(n)
         */
        rangeCheckForAdd(index);
        ensureCapacity(size * 2);

        for (int i = size; i > index; i--) {
            elements[i] = elements[i-1];
        }
        elements[index] = element;
        size++;
    }//size是数据规模

    /**
     * 删除index位置的元素
     * @param index
     * @return
     */
    public E remove(int index) {
        /**
         * 最好：O(1)
         * 最坏：O(n)
         * 平均：O(n)
         */
        rangeCheck(index);
        E old = elements[index];

        for (int i = index + 1; i < size; i++) {
            elements[i - 1] = elements[i];
        }
        elements[--size] = null;
        return old;
    }

    /**
     * 删除等于element的元素
     * @param element
     * @return
     */
    public void remove(E element) {
        remove(indexOf(element));
    }


    /**
     * 查看元素的索引
     * @param element
     * @return
     */
    public int indexOf(E element) {
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (elements[i] == null) {
                    return i;
                }
            }
        }else {
            for (int i = 0; i < size; i++) {
                if (element.equals(elements[i])) {
                    return i;
                }
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    /**
     * 保证要有capacity的容量
     * 扩容
     * @param capacity
     */
    private void ensureCapacity(int capacity){
        int oldCapacity = elements.length;
        if (oldCapacity > capacity) return;

        //新容量为旧容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];

        }
        elements = newElements;
        System.out.println(oldCapacity+"扩容为："+newCapacity);
    }


    @Override
    public String toString() {
        // size=3,[99,55]
        StringBuffer string = new StringBuffer();
        string.append("size=").append(size).append(",[");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }
            string.append(elements[i]);
//            if (i != (size - 1)) {
//                string.append(", ");
//            }
        }
        string.append("]");
        return string.toString();
    }
}
